package org.yzh.web.model.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.github.yezhihao.protostar.util.Bcd;
//import net.sf.json.JSONObject;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.yzh.protocol.commons.Charsets;
import org.yzh.web.commons.*;

import java.io.*;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Base64;


public class DeviceInfo {

    /** 签发日期 */
    private LocalDate issuedAt;
    /** 有效期 （日） */
    private int validAt;
    /** 车牌颜色 */
    private byte plateColor;
    /** 车牌号 */
    private String plateNo;
    /** 设备ID */
    private String deviceId;

    public DeviceInfo() {
    }

    public LocalDate getIssuedAt() {
        return issuedAt;
    }

    public void setIssuedAt(LocalDate issuedAt) {
        this.issuedAt = issuedAt;
    }

    public int getValidAt() {
        return validAt;
    }

    public void setValidAt(int validAt) {
        this.validAt = validAt;
    }

    public byte getPlateColor() {
        return plateColor;
    }

    public void setPlateColor(byte plateColor) {
        this.plateColor = plateColor;
    }

    public String getPlateNo() {
        return plateNo;
    }

    public void setPlateNo(String plateNo) {
        this.plateNo = plateNo;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }


    public static DeviceInfo formBytes(byte[] bytes) {
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
             DataInputStream dis = new DataInputStream(bis)) {
            System.out.println("DeviceInfo==formBytes===Arrays.toString=="+Arrays.toString(bytes));//字节数组打印

            DeviceInfo result = new DeviceInfo();
            byte[] temp;
            dis.read(temp = new byte[3]);
            System.out.println("Bcd.toDate(temp)"+Bcd.toDate(temp));
            result.setIssuedAt(Bcd.toDate(temp));

            result.setValidAt(dis.readUnsignedByte());
            int len = dis.readUnsignedByte();
            dis.read(temp = new byte[len]);
            result.setDeviceId(new String(temp, Charsets.GBK));

            return result;
        } catch (IOException e) {
            System.out.println("=============formBytes");
            throw new RuntimeException(e);
        }
    }

    public static byte[] toBytes(DeviceInfo deviceToken) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(32);
             DataOutputStream dos = new DataOutputStream(bos)) {

            dos.write(Bcd.from(deviceToken.getIssuedAt()));
            dos.writeByte(deviceToken.getValidAt());
            byte[] bytes = deviceToken.getDeviceId().getBytes(Charsets.GBK);
            dos.writeByte(bytes.length);
            dos.write(bytes);
            System.out.println("DeviceInfo==toBytes===Arrays.toString=="+Arrays.toString(bytes));//字节数组打印

            return bos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("issuedAt", issuedAt)
                .append("validAt", validAt)
                .append("plateColor", plateColor)
                .append("plateNo", plateNo)
                .append("deviceId", deviceId)
                .toString();
    }

    public static void main1(String[] args) {
        DeviceInfo device = new DeviceInfo();
        device.setIssuedAt(LocalDate.now());
        device.setValidAt(7);
        device.setPlateColor(Byte.parseByte("1"));
        device.setPlateNo("");
        device.setDeviceId("2005202365");
        byte[] bytes = DeviceInfo.toBytes(device);
        bytes = EncryptUtilsOld.encrypt(bytes);
        String token = Base64.getEncoder().encodeToString(bytes);
        System.out.println("DeviceInfo==main===token=="+token);

    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceId("141143673641");

        deviceInfo.setPlateColor((byte)1);
        deviceInfo.setPlateNo("AAAA");
        deviceInfo.setIssuedAt(LocalDate.now());
        deviceInfo.setValidAt(7);

/*        String json = JSONUtil.beanToJson(deviceInfo);
        System.out.println("json==="+json);
        try {
           String aa= AESUtils11.encryptAES(json);
            System.out.println("aa==="+aa);


           String bb=AESUtils11.decryptAES(aa);
            System.out.println("bb==="+bb);
            JSONObject jsonObject = JSONObject.parseObject(bb);

           // Object Object1 = JSONUtil.jsonToBean(bb, DeviceInfo.class);
            DeviceInfo deviceInfo2 = JSON.toJavaObject(jsonObject,DeviceInfo.class);
            System.out.println("deviceInfo2==="+deviceInfo2);
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        byte[] bytes = DeviceInfo.toBytes(deviceInfo);
        System.out.println(Arrays.toString(bytes));
        bytes = EncryptUtils.encrypt(bytes);
        String token = Base64.getEncoder().encodeToString(bytes);
        System.out.println(token);

        byte[] decode = Base64.getDecoder().decode("mu0OQRlGSCoe".getBytes("GBK"));
        byte[] decrypt = EncryptUtils.decrypt(decode);
        DeviceInfo device = DeviceInfo.formBytes(decrypt);
        System.out.println(deviceInfo);
        System.out.println(device);
        System.out.println("11111");


        //main2();
    }


    public static void main2() throws UnsupportedEncodingException {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceId("141143673641");

        deviceInfo.setPlateColor((byte)1);
        deviceInfo.setPlateNo("AAAA");
        deviceInfo.setIssuedAt(LocalDate.now());
        deviceInfo.setValidAt(7);

        byte[] bytes = DeviceInfo.toBytes(deviceInfo);
        System.out.println(Arrays.toString(bytes));
        bytes = EncryptUtilsOld.encrypt(bytes);
        String token = Base64.getEncoder().encodeToString(bytes);
        System.out.println(token);

        byte[] decode = Base64.getDecoder().decode(token.getBytes("GBK"));
        byte[] decrypt = EncryptUtilsOld.decrypt(decode);
        DeviceInfo device = DeviceInfo.formBytes(decrypt);
        System.out.println(deviceInfo);
        System.out.println(device);
        System.out.println("11111");
    }
}